package appnumerico.web.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import appnumerico.web.utils.WebUtils.Funcion;

@Component
public class FunctionInterceptor extends HandlerInterceptorAdapter {

	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		HttpSession session = request.getSession();
		
		String funcion = request.getParameter("fx");
		if (StringUtils.isNotBlank(funcion))
			WebUtils.setFuncion(session, Funcion.FX, funcion);
		
		if (WebUtils.getFuncion(session) == null) {
			response.sendRedirect(request.getContextPath());
			return false;
		}
		
		funcion = request.getParameter("gx");
		if (StringUtils.isNotBlank(funcion))
			WebUtils.setFuncion(session, Funcion.GX, funcion);
		
		return true; 
    }

	@Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
	
}
